package domain.entities;

import org.hibernate.LockMode;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * InvestigadorProtocoloCentro entities. Transaction control of the save(),
 * update() and delete() operations can directly support Spring
 * container-managed transactions or they can be augmented to handle
 * user-managed Spring transactions. Each of these methods provides additional
 * information for how to configure it for the desired type of transaction
 * control.
 * 
 * @see domain.entities.InvestigadorProtocoloCentro
 * @author MyEclipse Persistence Tools
 */

public class InvestigadorProtocoloCentroDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(InvestigadorProtocoloCentroDAO.class);
	// property constants
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	public void save(InvestigadorProtocoloCentro transientInstance) {
		log.debug("saving InvestigadorProtocoloCentro instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(InvestigadorProtocoloCentro persistentInstance) {
		log.debug("deleting InvestigadorProtocoloCentro instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public InvestigadorProtocoloCentro findById(
			domain.entities.InvestigadorProtocoloCentroId id) {
		log
				.debug("getting InvestigadorProtocoloCentro instance with id: "
						+ id);
		try {
			InvestigadorProtocoloCentro instance = (InvestigadorProtocoloCentro) getHibernateTemplate()
					.get("domain.entities.InvestigadorProtocoloCentro", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<InvestigadorProtocoloCentro> findByExample(
			InvestigadorProtocoloCentro instance) {
		log.debug("finding InvestigadorProtocoloCentro instance by example");
		try {
			List<InvestigadorProtocoloCentro> results = (List<InvestigadorProtocoloCentro>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

    public List findByProperty(String propertyName, Object value) {
        log
                .debug("finding InvestigadorProtocoloCentro instance with property: "
                        + propertyName + ", value: " + value);
        try {
            String queryString = "from InvestigadorProtocoloCentro as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByInvestigator(Involucrado value) {
        log
                .debug("finding InvestigadorProtocoloCentro instance with involucrado: "
                        + value.getNombre() + " " + value.getApellido() + ", id: " + value.getId());
        try {
            String queryString = "from InvestigadorProtocoloCentro as model where model.principalInvestigator.id = " + value.getId() + " OR model.subInvestigator.id = " + value.getId();
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<InvestigadorProtocoloCentro> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all InvestigadorProtocoloCentro instances");
		try {
			String queryString = "from InvestigadorProtocoloCentro";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public InvestigadorProtocoloCentro merge(
			InvestigadorProtocoloCentro detachedInstance) {
		log.debug("merging InvestigadorProtocoloCentro instance");
		try {
			InvestigadorProtocoloCentro result = (InvestigadorProtocoloCentro) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(InvestigadorProtocoloCentro instance) {
		log.debug("attaching dirty InvestigadorProtocoloCentro instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(InvestigadorProtocoloCentro instance) {
		log.debug("attaching clean InvestigadorProtocoloCentro instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static InvestigadorProtocoloCentroDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (InvestigadorProtocoloCentroDAO) ctx
				.getBean("InvestigadorProtocoloCentroDAO");
	}

    public List<InvestigadorProtocoloCentro> findAllByProtocolosCompromisopaises(List<Compromisopais> cps, List<Protocolo> protocolos){
        List<InvestigadorProtocoloCentro> result = getSession().createCriteria(InvestigadorProtocoloCentro.class)
                .createAlias("protocoloCentro", "pc", CriteriaSpecification.INNER_JOIN)
                .createAlias("pc.protocoloCompromisopais", "pcp", CriteriaSpecification.INNER_JOIN)
                .add(Restrictions.in("pcp.compromisopais", cps))
                .add(Restrictions.in("pcp.protocolo", protocolos))
                .list();
        return result;
    }

    public List<InvestigadorProtocoloCentro> findAllByProtocoloCompromisopais(ProtocoloCompromisopais pcp){
        String queryString = "from InvestigadorProtocoloCentro where id.protocoloCentroprotocoloCompromisopaiscompromisopaisid = " + pcp.getCompromisopais().getId() + " and id.protocoloCentroprotocoloCompromisopaisprotocoloid = '" + pcp.getProtocolo().getId() + "'";
        return getHibernateTemplate().find(queryString);
    }

    public List<InvestigadorProtocoloCentro> findAllByProtocoloCompromisopaisAndInvestigator(ProtocoloCompromisopais pcp, Integer investigatorId) {
        String queryString = "from InvestigadorProtocoloCentro where id.protocoloCentroprotocoloCompromisopaiscompromisopaisid = " + pcp.getCompromisopais().getId() + " and id.protocoloCentroprotocoloCompromisopaisprotocoloid = '" + pcp.getProtocolo().getId() + "' and id.investigadorid = " + investigatorId;
        return getHibernateTemplate().find(queryString);
    }
}